package com.augurit.smartform.web.component;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.smartform.service.IComponentDefinitionService;
import com.augurit.smartform.service.IComponentRegisterService;
import com.augurit.smartform.web.component.form.ComponentDefinitionForm;
import com.augurit.smartform.web.component.form.ComponentRegisterForm;

public class ComponentRegisterAction extends ExtCrudActionSupport<ComponentRegisterForm>{
	@Autowired
	private IComponentRegisterService componentRegisterService;
	@Autowired
	private IComponentDefinitionService componentDefinitionService;
	
	private ComponentRegisterForm form;
	private Long id;

	@Override
	public String delete() throws Exception {
		componentRegisterService.deleteAndUnregist(form);
		return null;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id!=null){
			form=componentRegisterService.get(id);
		}else{
			form=new ComponentRegisterForm();
		}
	}
	
	/**
	 * 保存之前进行名称检查
	 */
	public void beforeSave(){
		ComponentRegisterForm checkForm=componentRegisterService.queryByName(form.getName());
		if(checkForm==null){
			this.extRenderSuccess();
		}else if(checkForm.getId().equals(form.getId())){
			this.extRenderSuccess();
		}else{
			this.extRenderFailture();
		}
	}
	
	/**
	 * 保存并注册某个目录
	 */
	public String save() throws Exception {
		componentRegisterService.saveAndRegist(form);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 禁用组件目录
	 */
	public void disable(){
		componentRegisterService.disable(form);
		this.extRenderSuccess();
	}
	
	/**
	 * 禁用组件目录
	 */
	public void enable(){
		componentRegisterService.enable(form);
		this.extRenderSuccess();
	}
	
	/**
	 * 判断某个目录下面是否有组件
	 */
	public void hasComponent(){
		List<ComponentDefinitionForm> list=componentDefinitionService.getDefinitionByRegisterId(id);
		if(list!=null&&list.size()>0){
			this.extRenderSuccess();
		}else{
			this.extRenderFailture();
		}
	}

	public ComponentRegisterForm getModel() {
		return form;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
